`this` in global scope in ECMAScript 6
Posted
by
Nathan Wall
on Stack Overflow
See other posts from Stack Overflow
or by Nathan Wall
Published on 2012-11-16T22:25:06Z
Indexed on
2012/11/17
5:00 UTC
Read the original article
Hit count: 194
I've tried looking in the ES6 draft myself, but I'm not sure where to look:
Can someone tell me if this
in ES6 necessarily refers to the global object? Will this object have same members as the global scope?
If you could answer for ES5 that would be helpful as well.
I know this
in global scope refers to the global object in the browser and in most other ES environments, like Node. I just want to know if that's the defined behavior by the spec or if that's extended behavior that implementers have added (and if this behavior will continue in ES6 implementations). In addition, is the global object always the same thing as the global scope? Or are there distinctions?
Update - Why I want to know: I am basically trying to figure out how to get the global object reliably in ES5 & 6. I can't rely on window
because that's specific to the browser, nor can I rely on global
because that's specific to environments like Node. I know this
in Node can refer to module
in module scope, but I think it still refers to global
in global scope. I want a cross-environment ES5 & 6 compliant way to get the global object (if possible). It seems like in all the environments I know of this
in global scope does that, but I want to know if it's part of the actual spec (and so reliable across any environment that I may not be familiar with).
I also need to know if the global scope and the global object are the same thing by the spec. In other words will all variables in global scope be the same as globalobject.variable_name
?
© Stack Overflow or respective owner